package com.zw.pdm.module.est.controller;

import com.zw.pdm.annotation.RepeatCommit;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.est.model.CraMakeSheetModel;
import com.zw.pdm.module.est.service.ChaWarnService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 变更提醒
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/est/cha")
@SuppressWarnings("all")
public class ChaWarnController extends BaseController {

    @Autowired
    private ChaWarnService chaWarnService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;


    @GetMapping
    @RequiresPermissions(value = {"est:cha:view"}, logical = Logical.OR)
    public String page(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "est/cha/page";
    }

    /** 列表 */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult cardList(CraMakeSheetModel model) {
        startPage();
        List<CraMakeSheetModel> list = chaWarnService.findWithChaWarn(model);
        return jsonResult(list);
    }

    /** 变更完结 */
    @ResponseBody
    @RepeatCommit
    @PostMapping("/handler")
    @RequiresPermissions({"est:cha:edit"})
    public JsonResult handler(Integer id) {
        return jsonResult(chaWarnService.handler(id));
    }

    /** 变更页面 */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"est:cha:edit"})
    public String editCard(@PathVariable("id") Integer id, Model model) {
        Card card = chaWarnService.getCardById(id);
        model.addAttribute("card", card);
        model.addAttribute("details", chaWarnService.findCardDetailAndTranByCardId(id));
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("parts", sheetService.findPartByProjectCode(card.getProCode(), "外观".equals(card.getSpecial()) ? null: card.getSpecial()));
        model.addAttribute("drawNos", sheetService.findDrawNoByProCodeAndPartAndId(card.getProCode(), card.getPartId(), card.getCraMakeDetailId()));
        model.addAttribute("routes", dictCommonService.findRoutes());
        model.addAttribute("processes", dictCommonService.findProcesses());
        model.addAttribute("equNames", dictCommonService.findEqu());
        model.addAttribute("verifys", dictCommonService.findPersonByCategory("工艺员"));
        return "est/cha/edit";
    }

    /** 保存工艺卡 */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions(value = {"est:cha:edit"}, logical = Logical.OR)
    public JsonResult update(Card card, Integer[] detailId, String[] route, Integer[] orderNo, String[] process, String[] equName, String[] craftContent) {
       try {
           return jsonResult(chaWarnService.update(card, detailId, route, orderNo, process, equName, craftContent));
       }catch (Exception e){
           return error("未修改内容，不可保存！");
       }
    }

    /** 变更页面 */
    @GetMapping("/info/{id}")
    public String info(@PathVariable("id") Integer id) {
        Integer hrefId = chaWarnService.findHrefId(id);
        return "redirect:/craft/receive//info/" + hrefId + "/2";
    }

    /** 变更页面 */
    @GetMapping("/info2/{id}")
    public String info2(@PathVariable("id") Integer id) {
        Integer hrefId = chaWarnService.findHrefId2(id);
        return "redirect:/craft/receive//info/" + hrefId + "/0";
    }

}
